package com.amat0urheu.util;

import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import javax.crypto.Cipher;

public class RSAUtil {
    static  String publicKeyString = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqXAeTTRUeejvct8qzfJy\n" +
            "QJaJOQUd4kg8QgClo+oba7Aw/0VHxPz7RFqflIAKKCNQYgGHwMpgE0idvAMW6vIY\n" +
            "Y6VYP+HbhXbADdpE1k1cl3sQ64p117L7DCWnRWUuKDY1dljIp4+TAdCEv6Z9yn3o\n" +
            "kp1V3Ewr4IaNn5mGfwMy0PSrLWWV3ubHQJ58yH6eAOE1jhSiuFOwM8WrEP+DP2O0\n" +
            "2z43FQ1WWQVArsNzl0mR8JleKRUQxrxlHdwDZB704xgh/cIUSaef11yNIZMjSa39\n" +
            "ZyNR9Lo9mc2PuqigvEg6lzM7P4xUqCYbpD7v5fT4EcBZgZWpLWplpuhiPbwuSAjt\n" +
            "bwIDAQAB"; // 替换为你的公钥字符串
    static String privateKeyString = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCpcB5NNFR56O9y\n" +
            "3yrN8nJAlok5BR3iSDxCAKWj6htrsDD/RUfE/PtEWp+UgAooI1BiAYfAymATSJ28\n" +
            "Axbq8hhjpVg/4duFdsAN2kTWTVyXexDrinXXsvsMJadFZS4oNjV2WMinj5MB0IS/\n" +
            "pn3KfeiSnVXcTCvgho2fmYZ/AzLQ9KstZZXe5sdAnnzIfp4A4TWOFKK4U7AzxasQ\n" +
            "/4M/Y7TbPjcVDVZZBUCuw3OXSZHwmV4pFRDGvGUd3ANkHvTjGCH9whRJp5/XXI0h\n" +
            "kyNJrf1nI1H0uj2ZzY+6qKC8SDqXMzs/jFSoJhukPu/l9PgRwFmBlaktamWm6GI9\n" +
            "vC5ICO1vAgMBAAECggEANYZsnBj+4rAaDGDBZePEAfFMEg4yvYvyNSt9KAXJecBk\n" +
            "Cg/5txnvyk0SglfcXYeiWgN/vgBwT0cuRPl44OlvmPLfqPaRYVz9GKMuAcDYkiXX\n" +
            "V4Ea7vPkaJbwe3SWGy/cvbEYWg558yXK+bv/mtshgEdhm0EYz0nSLWvTWAZLpERS\n" +
            "ooetIX6VOpoaAegVj95nccSUpO/kI5Wyz6Mwpf1gJFjodrAHhpQUMOrNMUx3/MAU\n" +
            "qJFc85BuQWL0k2O6ipP9n/28noN0qSGlcX+6I8UFz7k2OH9IiQYE2FJpQEJ5bPFg\n" +
            "lySi8e5amP3clrXWlhOWJ3Cglq6yBG/Bhen2mkKo8QKBgQDZ6XSoC7i8Se0r78Y5\n" +
            "vplOrtCZI3x9UKRNaICuJ/aJ9Qh4CC1KvS2qKeMW38oy45kEKqAR/b7wlzRD/4lr\n" +
            "A78qG1XZQiqwgkTMFlb9RvVm5BHFsPbIpqU3B25EnMHmBQodps38PDB7Bm1jVCNS\n" +
            "+WdcrBLl/ASLY2Mh550BvJG62QKBgQDHDa2Xaeec+LRAUGDzTUx/dWGC52XCD1BT\n" +
            "GBFrkDYBC8NOp3kgNtvxhAkhIHvOeOm68VZMSsWqN/jNkltfbkWp2M93p0wHGo0n\n" +
            "8lQ6ccW2d5sG0kSc0waKjZKBYlA8XeUJqEJehe92ghb5MTP+y2IsGBatMO/UNyhv\n" +
            "+/W+YQlthwKBgGWHj3HonDcmXpnVCs/GzVlKvChNz2f31cW3RhVey1C12S9L6AZl\n" +
            "pEwfXg4/UM9rGdVGIvSe/ILOgGyaOCWjRkNRHyut14QL/g/07KzvjdFVvC4W6LTO\n" +
            "aDX+gVL4kqMgf/kWyeyXAqOfeAIKpra3O4CDL/718Awdz4dxicbQXDnpAoGBALsM\n" +
            "1nBvm6+9Yh0Bq2lW3Sj1E/HgIpZur4u2FUCf3q+y7xBtffxQsPROoAmaLd2DcBg1\n" +
            "C2Rb602elVYQT37o79vAmo0VD/Oo9rZwGMhm+2URFgQd+U3pW789dEvTKBTiIwWl\n" +
            "MOEEOVeCvbXjj7nCQ3muFTkgwG4Kexxoh7AkHajnAoGAQBvjSAPcdsPV3I5iJzKN\n" +
            "ph+/eqJHhTn4Oa+PJeBs1RkJKnQ8/55V6THEfO/Y/FOlSCiJDl39+Y8ksiVLfIQk\n" +
            "XIx6ivIDtEFNebh8mZcNg2pdR82GWbLu5COdvRBj8jP9hHTOeXyuIee+tya3DlEk\n" +
            "ngCgTuEcY6ihCnXF8abI46w="; // 替换为你的私钥字符串
    public static byte[] encrypt(PublicKey publicKey, byte[] inputData) throws Exception {
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        return cipher.doFinal(inputData);
    }

    public static byte[] decrypt(PrivateKey privateKey, byte[] inputData) throws Exception {
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        return cipher.doFinal(inputData);
    }

    public static PublicKey getPublicKey(String publicKeyString) throws Exception {
        byte[] keyBytes = java.util.Base64.getDecoder().decode(publicKeyString);
        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        return keyFactory.generatePublic(keySpec);
    }

    public static PrivateKey getPrivateKey(String privateKeyString) throws Exception {
        byte[] keyBytes = java.util.Base64.getDecoder().decode(privateKeyString);
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        return keyFactory.generatePrivate(keySpec);
    }

    public void test() {


        try {
            PublicKey publicKey = getPublicKey(publicKeyString);
            PrivateKey privateKey = getPrivateKey(privateKeyString);

            String data = "Hello, RSA!";
            byte[] encryptedData = encrypt(publicKey, data.getBytes());
            byte[] decryptedData = decrypt(privateKey, encryptedData);

            System.out.println("原始数据: " + data);
            System.out.println("加密后数据: " + new String(encryptedData));
            System.out.println("解密后数据: " + new String(decryptedData));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }
}